package md.mru.core.service;

import md.mru.core.common.BaseDtoMapper;
import md.mru.core.domains.*;
import md.mru.core.dtos.*;
import md.mru.core.repositories.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author sergiu.marin
 * @since 01.04.2014
 */
@Service
public class AdditionalDataService {

    @Autowired
    @Qualifier(value = "dtoMapper")
    private BaseDtoMapper mapper;
    @Autowired
    private CitizenRepository citizenRepository;
    @Autowired
    private CitizenshipRepository citizenshipRepository;
    @Autowired
    private DocumentRepository documentRepository;
    @Autowired
    private DocumentTypeRepository documentTypeRepository;
    @Autowired
    private FamilyMemberRepository familyMemberRepository;
    @Autowired
    private FamilyRelationRepository familyRelationRepository;

    public List<CitizenDto> getCitizens(long folderId) {
        List<Citizen> citizens = citizenRepository.getCitizens(folderId);
        return mapper.mapList(citizens, CitizenDto.class);
    }

    public List<CitizenshipDto> getCitizenShips() {
        List<Citizenship> citizenships = citizenshipRepository.findAll();
        return mapper.mapList(citizenships, CitizenshipDto.class);
    }

    public long saveCitizen(CitizenDto citizenDto) {
        Citizen citizen = mapper.map(citizenDto, Citizen.class);
        return citizenRepository.save(citizen).getId();
    }

    public void deleteCitizen(long citizenId) {
        citizenRepository.delete(citizenId);
    }

    public List<DocumentDto> getDocuments(long folderId) {
        List<Document> documents = documentRepository.getDocument(folderId);
        return mapper.mapList(documents, DocumentDto.class);
    }

    public List<DocumentTypeDto> getDocumentTypes() {
        List<DocumentType> documentTypes = documentTypeRepository.findAll();
        return mapper.mapList(documentTypes, DocumentTypeDto.class);
    }

    public long saveDocument(DocumentDto documentDto) {
        Document document = mapper.map(documentDto, Document.class);
        return documentRepository.save(document).getId();
    }

    public void deleteDocument(long documentId) {
        documentRepository.delete(documentId);
    }

    public List<FamilyMemberDto> getFamilyMembers(long folderId) {
        List<FamilyMember> familyMembers = familyMemberRepository.getFamilyMembers(folderId);
        return mapper.mapList(familyMembers, FamilyMemberDto.class);
    }

    public List<FamilyRelationDto> getFamilyRelations() {
        List<FamilyRelation> familyRelations = familyRelationRepository.findAll();
        return mapper.mapList(familyRelations, FamilyRelationDto.class);
    }

    public long saveFamilyMember(FamilyMemberDto familyMemberDto) {
        FamilyMember familyMember = mapper.map(familyMemberDto, FamilyMember.class);
        return familyMemberRepository.save(familyMember).getId();
    }

    public void deleteFamilyMember(long familyMemberId) {
        familyMemberRepository.delete(familyMemberId);
    }
}
